package com.manager.web.system;

import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.manager.base.BaseAction;
import com.manager.pojo.system.SysPrivilege;
import com.manager.util.DateUtil;
import com.manager.util.Log;
import com.opensymphony.xwork2.Action;

public class SysPrivilegeAction extends BaseAction {
	private SysPrivilege sysPrivilege;
	
	/*
	 * 添加权限
	 */
	public String addPrivilege(){
		int loginUserId = super.getLoginUserId();
		PrintWriter p = null;
		
		try{
			p = response.getWriter();
			
			//查询权限名是否已存在
			int total = super.baseService.queryInt("select count(1) from sys_privilege where name = ?", sysPrivilege.getName());
			if(total == 1){
				p.print("{\"flag\":false,\"msg\":\"该权限名已存在\"}");
			}else{
				SysPrivilege sp = new SysPrivilege();
				sp.setName(sysPrivilege.getName());
				sp.setUrl(sysPrivilege.getUrl());
				sp.setMenuId(sysPrivilege.getMenuId());
				sp.setCreateById(loginUserId);
				sp.setCreateDateTime(DateUtil.getCurrentDateTime(true));
				sp.setUpdateById(loginUserId);
				sp.setUpdateDateTime(DateUtil.getCurrentDateTime(true));
				super.baseService.save(sp);
				
				p.print("{\"flag\":true}");
			}
		}catch(Exception e){
			p.print("{\"flag\":false}");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		return Action.NONE;
	}
	
	/*
	 * 删除权限
	 */
	public String deletePrivilege(){
		int sysPrivilegeId = sysPrivilege.getId();
		PrintWriter p = null;
		
		try{
			p = response.getWriter();
			//删除基本信息
			super.baseService.delete("delete from sys_privilege where id = ?", sysPrivilegeId);
			//删除sys_role_privilege表中的所有该权限的信息
			super.baseService.delete("delete from sys_role_privilege where privilege_id = ?", sysPrivilegeId);
			
			p.print("{\"flag\":true}");
		}catch(Exception e){
			p.print("{\"flag\":false}");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		return Action.NONE;
	}
	
	/*
	 * 由id查询privilege
	 */
	public String queryPrivilegeById(){
		int sysPrivilegeId = sysPrivilege.getId();
		String hql = "FROM SysPrivilege p WHERE p.id = ?";
		SysPrivilege sysPrivilege = (SysPrivilege)super.baseService.getObject(hql, sysPrivilegeId);
		this.sysPrivilege = sysPrivilege;
		super.forward("system/privilegeUpdate.jsp");
		return Action.NONE;
	}
	
	/*
	 * 更新privilege
	 */
	public String updatePrivilege(){
		PrintWriter p = null;
		try{
			p = response.getWriter();
			
			//查询除了该ID的权限，其它的所有权限中该权限名是否存在
			int total = super.baseService.queryInt("select count(1) from sys_privilege where name = ? and id != ?", sysPrivilege.getName(), sysPrivilege.getId());
			if(total == 1){
				p.print("{\"flag\":false,\"msg\":\"该权限名已存在\"}");
			}else{
				SysPrivilege sp = (SysPrivilege)super.baseService.getObject(SysPrivilege.class, sysPrivilege.getId());
				sp.setName(sysPrivilege.getName());
				sp.setUrl(sysPrivilege.getUrl());
				sp.setMenuId(sysPrivilege.getMenuId());
				sp.setUpdateById(super.getLoginUserId());
				sp.setUpdateDateTime(DateUtil.getCurrentDateTime(true));
				super.baseService.update(sp);
				
				p.print("{\"flag\":true}");
			}
		}catch(Exception e){
			p.print("{\"flag\":false}");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		
		return Action.NONE;
	}
	
	/*
	 * getter and setter
	 */
	public SysPrivilege getSysPrivilege() {
		return sysPrivilege;
	}

	public void setSysPrivilege(SysPrivilege sysPrivilege) {
		this.sysPrivilege = sysPrivilege;
	}
}
